$('#location').live('pageinit', function(e) {
    var selectMov = false;
    var startX;
    var selectLeft;
    var locImgWid;
    var isFirst = true;

    $('#location').bind('pageshow', function(e) {
        if (isFirst) {
            isFirst = false;
            docWid = $(document).width();
            contWid = $('div[data-role="content"]').width();
            locImgWid = $('#selectLoc ul li img').width();
            $('#selectLoc').css('left', contWid / 2 - locImgWid / 2 + 'px');
        }
    })

    $('#selectCurrent a img, #selectLoc a img').bind('click', function(e) {
        local = $(this).attr('data-local');
    })

    $('#selectLoc').bind('vmousedown', function(e) {
        e.preventDefault();
        selectMov = true;
        startX = e.clientX;
        selectLeft = Number($('#selectLoc').css('left').replace('px', ''));
    })

    $(document).bind('vmouseup', function(e) {
        selectMov = false;
    })

    $(document).bind('vmousemove', function(e) {
        if (selectMov) {
            e.preventDefault();
            var firstCenter = $('#selectLoc ul li:first').offset().left + locImgWid / 2;
            var lastCenter = $('#selectLoc ul li:last').offset().left + locImgWid / 2;
            if (docWid / 2 < firstCenter - startX + e.clientX) {
                $('#selectLoc').css('left', contWid / 2 - locImgWid / 2 + 'px');
                $('#selectCurrent a img').attr('src', $('#selectLoc ul li:first img').attr('src'));
                $('#selectCurrent a img').attr('data-local', $('#selectLoc ul li:first img').attr('data-local'));
            } else if (docWid / 2 > lastCenter - startX + e.clientX) {
                $('#selectLoc').css('left', contWid / 2 - locImgWid / 2 + firstCenter - lastCenter + 'px');
                $('#selectCurrent a img').attr('src', $('#selectLoc ul li:last img').attr('src'));
                $('#selectCurrent a img').attr('data-local', $('#selectLoc ul li:last img').attr('data-local'));
            } else {
                $('#selectLoc').css('left', selectLeft - startX + e.clientX + 'px');
                $('#selectLoc ul li').each(function() {
                    if ($(this).offset().left < docWid / 2 && docWid / 2 < $(this).offset().left + locImgWid) {
                        $('#selectCurrent a img').attr('src', $(this).find('img').attr('src'));
                        $('#selectCurrent a img').attr('data-local', $(this).find('img').attr('data-local'));
                        return;
                    }
                })
            };
        }
    })
})

$('#menu').live('pageinit', function(e) {
    $('#menu h1').html(area[local]);
})

$('#prac').live('pageinit', function(e) {
    $.each(chapter, function(key, value) {
        if (key == "0" || key == local || key.indexOf(".") >= 0) {
            var li = document.createElement('li');
            var a = document.createElement('a');
            a.innerHTML = value;
            a.setAttribute('href', 'pracDetail.html');
            a.setAttribute('data-chapter', key);
            a.addEventListener('click', function(e) {
                chapterName = this.getAttribute('data-chapter');
            }, false)
            li.appendChild(a);
            document.getElementById('chapter').appendChild(li);
        }
    })
    $('#chapter').listview('refresh');
})

$('#pracDetail').live('pageinit', function(e) {
    var curTime = 0;
    var curTimeMin;
    var curTimeSec;
    var qNumber = cq[chapterName][0];
    var qCount = cq[chapterName][1];
    var qCurrent = 1;
    $('#pracTotal').html('共' + qCount + '题');
    $('#pracDetail h1').html(chapter[chapterName]);
    $('#jumpSlid').attr('max', qCount);

    loadQues(qCurrent, qNumber, qCount);

    timeCount = setInterval(function() {
        curTime = curTime + 1;
        curTimeMin = Math.floor(curTime / 60);
        curTimeSec = curTime % 60;
        $('#pracTime').html((curTimeMin > 9 ? curTimeMin : '0' + curTimeMin) + ':' + (curTimeSec > 9 ? curTimeSec : '0' + curTimeSec));
    }, 1000)

    $('#pracDetail').bind('pagehide', function(e) {
        clearInterval(timeCount);
    })

    $('#prev').bind('click', function() {
        if (qCurrent > 1) {
            qNumber = qNumber - 1;
            qCurrent = qCurrent - 1;
            loadQues(qCurrent, qNumber, qCount);
        }
    })
    $('#next').bind('click', function() {
        if (qCurrent < qCount) {
            qNumber = qNumber + 1;
            qCurrent = qCurrent + 1;
            loadQues(qCurrent, qNumber, qCount);
        }
    })

    $('#jump').bind('click', function() {
        if ($('#jumpContain').is(":visible")) {
            $('#jumpContain').hide();
        } else {
            $('#jumpContain').show();
        }
    })
    $('#jumpGo').bind('click', function() {
        
        var qCurrentT = Number($('#jumpSlid').val());
        qNumber=qNumber+qCurrentT-qCurrent;
        qCurrent=qCurrentT;
        $('#jumpSlid').val(qCurrent)
        loadQues(qCurrent, qNumber, qCount);
        $('#jumpContain').hide();
    })
    $('#jumpCancel').bind('click', function() {
        $('#jumpContain').hide();
    })
})